package cn.edu.zzuli;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Date: 2021/3/10 21:45
 * @Author: 绯色下午茶
 * @Description: 异步回调
 */

public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "没有返回，");
        });
        completableFuture.get();

        //异步回调
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "有回调");
            int age = 10/0;
            return 1024;
        });
        Integer integer = integerCompletableFuture.whenComplete((o1, o2) -> {
            System.out.println("**************" + o1);
            System.out.println("**************" + o2);

        }).exceptionally(f -> {
            System.out.println("**********exception       " + f.getMessage());
            return 444;
        }).get();
        System.out.println(integer);


    }
}
